package com.wikidot.designpatterns.memento;

/**
 * Originator part of the memento pattern. This class is going to be changed by
 * the client application but the client wants to keep track of the changes
 * made. This class provides state copies that can be archived and then later
 * used to state restoration.
 */
public class Image {
	/**
	 * The actual view area of the picture. At the begining nothing is visible.
	 * This attribute is comprised into state of the image.
	 */
	private Dimension actual = new Dimension(0, 0);

	/**
	 * Informs whether given image is visible at all. At the begining it is
	 * visible. This attribute is comprised into state of the image.
	 */
	private boolean hidden = false;

	/**
	 * Update method that changes state of the image.
	 * 
	 * @param dimension
	 *            Defines visible area.
	 * @param hidden
	 *            Defines overall visibility of the image
	 */
	public void setVisibility(Dimension dimension, boolean hidden) {
		if (dimension != null) {
			// do not change size of the visible area
			this.actual = dimension;
		}
		this.hidden = hidden;
	}

	/**
	 * This method provides actual state of the image in encapsulating object.
	 * 
	 * @return Actual state representation.
	 */
	public LogRecord rememberSettings() {
		return new LogRecord(actual, hidden);
	}

	/**
	 * Sets actual state to some previously archived one.
	 * 
	 * @param record
	 *            State to be restored.
	 */
	public void restore(LogRecord record) {
		this.actual = record.getDimension();
		this.hidden = record.isHidden();
	}

	/**
	 * This is only informative method that hust prints actual state of the
	 * image.
	 */
	public void display() {
		if (!hidden) {
			System.out.println("Area " + actual + " is invisible ");
		} else {
			System.out.println("Area " + actual + " is visible");
		}
	}
}
